package cn.qingyun.gis.modules.sftp.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.sftp.config.SftpProperties;
import cn.qingyun.gis.modules.sftp.service.IFileSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/sftp")
@Api(value = "SFTP 服务器接口", tags = {"SFTP 服务器接口"})
@Slf4j
public class SftpController {
    @Resource
    private IFileSystemService iFileSystemService;

    @Resource
    private SftpProperties config;
    @Value("${qingyun.sftp.transfer.enabled:false}")
    private Boolean transferEnabled;


    /**
     * 文件下载
     *
     * @return
     */
    @SneakyThrows
    @PostMapping("/upload")
    @ApiOperation(value = "SFTP-文件上传")
    public Result<?> upload(@RequestParam("file") MultipartFile file, String filePath, String fileName) {
        String path = "/home/gis1/" + filePath + "/" + fileName;
        boolean res = iFileSystemService.uploadFiles(path, file);
        if (res) {
            return Result.ok("上传成功");
        }
        return Result.error("上传失败");
    }

    @GetMapping("/downloadRegion")
    @ApiOperation(value = "使用按钮调用SFTP-业务区划下载")
    public Result<?> downloadRegion() {
        iFileSystemService.downloadRegion();
        return Result.okData("正在使用SFTP进行最新业务区划下载");
    }

}

